package com.domian.news;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.domian.news.DataParser.BlogsListXmlParser;
import com.domian.news.adapters.BlogListAdapter;
import com.domian.news.models.Blog;
import com.domian.news.utils.ParamName;

import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by hhj on 2016-06-19.
 */
public class HomeFragment extends Fragment {

    //数据源
    private List<Blog> mBlogs;
    private BlogListAdapter mBlogListAdapter;
    private RecyclerView mRecyclerView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);

        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        initData(1, 20);
        return view;
    }

    public static HomeFragment newInstance(String param) {
        HomeFragment fragment = new HomeFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ParamName.ARG_PARAM1, param);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void initData(int page, int num) {

        String url = "http://wcf.open.cnblogs.com/blog/sitehome/paged/"+page+"/"+num;
        StringRequest stringRequest = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        ByteArrayInputStream inputStream = new ByteArrayInputStream(response.getBytes());

                        try {
                            mBlogs = BlogsListXmlParser.getListBlogs(inputStream, "utf-8");

                            mBlogListAdapter = new BlogListAdapter(getActivity(), mBlogs);
                            mRecyclerView.setAdapter(mBlogListAdapter);

                        } catch (XmlPullParserException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue mQueue = Volley.newRequestQueue(this.getContext());
        mQueue.add(stringRequest);
    }
}
